package com.misyi.designpattern.command;

/**
 * 命令模式
 *
 * @author licong
 * @date 2021-09-14 下午4:34
 */
public class CommandPatternDemo {

    public static void main(String[] args) {
        Command commandA = new CommandA();
        Command commandB = new CommandB();
        Command commandC = new CommandC();

        Invoker invoker = new Invoker();
        invoker.setCommand(commandA);
        invoker.execute();
        invoker.setCommand(commandB);
        invoker.execute();
        invoker.setCommand(commandC);
        invoker.execute();
    }


    public static class Invoker {
        private Command command;

        public void setCommand(Command command) {
            this.command = command;
        }

        public void execute() {
            System.out.println("执行命令前的一些其他逻辑");
            command.execute();
            System.out.println("执行命令后的一些其他逻辑");
        }
    }


    public interface Command {
        void execute();
    }

    public static class CommandA implements Command {

        @Override
        public void execute() {
            System.out.println("命令A的功能逻辑");
        }
    }

    public static class CommandB implements Command {

        @Override
        public void execute() {
            System.out.println("命令B的功能逻辑");
        }
    }

    public static class CommandC implements Command {

        @Override
        public void execute() {
            System.out.println("命令C的功能逻辑");
        }
    }

}
